<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
          <a-row>
            <a-col :span="24">
              <a-form-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-select-depart v-decorator="['departId', validatorRules.departId]" :multi="false" placeholder="请选择部门" @change="departChange"/>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="用户" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <div style="display: flex">
                  <a-select
                    mode="multiple"
                    placeholder="请选择用户"
                    v-decorator="['userId', validatorRules.userId]"
                    :disabled="disabled"
                    style="margin-top: 3px;"
                  />
                  <a-button type="primary" @click="selectUser" style="margin-left: 12px;margin-top: 3px;">选择</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
    </a-spin>
    <oaofficialdoc-select-user-model ref="oaofficialdocSelectUserModel" @ok="review"></oaofficialdoc-select-user-model>
  </a-modal>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import { duplicateCheck,queryUserByDepId } from '@/api/api'
  import OaofficialdocSelectUserModel from './OaofficialdocSelectUserModel'
  export default {
    name: "OaOfficialdocDepartUserModal",
    components: {
      JSelectDepart,
      OaofficialdocSelectUserModel
    },
    data () {
      return {
        title:'新增',
        width:1000,
        visible: false,
        disableSubmit: false,
        disabled:true,
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          departId: {
            rules: [
              { required: true, message: '请输入部门!'},
              {validator: this.departId}
            ]
          },
          userId: {
            rules: [
              { required: true, message: '请输入用户!'},
            ]
          },
        },
        url: {
          add: "/eoa/officialdoc/oaOfficialdocDepartUser/add",
          edit: "/eoa/officialdoc/oaOfficialdocDepartUser/edit",
          queryById: "/eoa/officialdoc/oaOfficialdocDepartUser/queryById"
        },
        ids:"",
      }
    },
    methods: {
      add () {
        this.edit({})
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.ids=this.model.userId;
        if(this.model.userId_dictText){
          this.model.userId=this.model.userId_dictText.split(",");
        }

        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'departId','userId'))
        })
      },
      close () {
        this.$emit('ok');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData.userId=this.ids;
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.close();
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      async getUserList(param){

      },
      departId(rule, value, callback){
        var params = {
          tableName: "oa_officialdoc_depart_user",
          fieldName: "depart_id",
          fieldVal: value,
          dataId: this.model.id,
        };
        duplicateCheck(params).then((res)=>{
          if(res.success){
            callback();
          }else{
            callback(res.message);
          }
        });
      },
      selectUser(){
        let departId = this.form.getFieldValue("departId")
        if(departId){
          var param=[];
          param.ids=this.ids.split(",");
          param.departId=departId;
          this.$refs.oaofficialdocSelectUserModel.loadData(param);
        }else{
          this.$message.warning("请选择部门！");
        }
      },
      review(data){
        this.form.setFieldsValue({'userId':data.realname});
        this.ids=data.ids;
      },
      departChange(){
        this.ids="";
        this.form.setFieldsValue({'userId':[]});
      }
    }
  }
</script>